package com.sky.controller.admin;

import com.sky.annotation.AutoInsertFill;
import com.sky.constant.JwtClaimsConstant;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.dto.PasswordEditDTO;
import com.sky.entity.Employee;
import com.sky.properties.JwtProperties;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.EmployeeService;
import com.sky.utils.JwtUtil;
import com.sky.vo.EmployeeLoginVO;
import com.sky.vo.EmployeeOV;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 员工管理
 */
@Api(tags = "员工管理接口")
@RestController
@RequestMapping("/admin/employee")
@Slf4j
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private JwtProperties jwtProperties;

    /**
     * 登录
     *
     * @param employeeLoginDTO the employee login dto
     * @return result result
     */
    @ApiOperation("员工登录")
    @PostMapping("/login")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
        log.info("员工登录：{}", employeeLoginDTO);

        Employee employee = employeeService.login(employeeLoginDTO);

        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID, employee.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);

        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                .id(employee.getId())
                .userName(employee.getUsername())
                .name(employee.getName())
                .token(token)
                .build();

        return Result.success(employeeLoginVO);
    }

    /**
     *  修改密码
     *
     * @param passwordEditDTO the password edit dto
     * @return the result
     */
    @ApiOperation("修改密码")
    @PutMapping("/editPassword")
    public Result<?> editPassword(@RequestBody PasswordEditDTO passwordEditDTO){
        employeeService.editPassword(passwordEditDTO);
        return Result.success();
    }


    /**
     * 退出
     *
     * @return result result
     */
    @ApiOperation("退出")
    @PostMapping("/logout")
    public Result<String> logout() {
        return Result.success();
    }


    /**
     * 员工分页查询
     *
     * @param epqDTO the epq dto
     * @return the result
     */
    @ApiOperation("员工分页查询")
    @GetMapping("/page")
    public Result<PageResult> pageList(EmployeePageQueryDTO epqDTO) {
        PageResult pageResult = employeeService.pageList(epqDTO);
        return Result.success(pageResult);
    }


    /**
     * 创建员工
     *
     * @param employeeDTO 员工参数
     * @return 状态 result
     */
    @ApiOperation("员工管理")
    @AutoInsertFill
    @PostMapping
    public Result<?> create(@RequestBody EmployeeDTO employeeDTO) {
        employeeService.create(employeeDTO);
        return Result.success();
    }

    /**
     * 更新员工状态.
     *
     * @param status the status
     * @param id     the id
     * @return the result
     */
    @ApiOperation("更新员工状态")
    @PostMapping("/status/{status}")
    public Result<?> updateStatus(@PathVariable Integer status,Long id){
        employeeService.updateStatus(status,id);
      return Result.success();
    }

    /**
     * 根据id查询回显
     *
     * @param id the id
     * @return the result
     */
    @ApiOperation("根据id查询回显")
    @GetMapping("/{id}")
    public Result<EmployeeOV> findEmpById(@PathVariable Long id){
          EmployeeOV employeeOV = employeeService.findEmpById(id);
          return Result.success(employeeOV);
    }


    /**
     * 根据id对员工信息进行修改
     *
     * @param employeeDTO the employee dto
     * @return the result
     */
    @ApiOperation("根据id对员工信息进行修改")
    @PutMapping
    public Result<?> updateById(@RequestBody EmployeeDTO employeeDTO){
        employeeService.updateById(employeeDTO);
        return Result.success();
    }

}
